package logic

import (
	"context"
	"fmt"
	"home-manage/model"
	"time"

	"home-manage/homerpc/homerpc"
	"home-manage/homerpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AppointmentAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAppointmentAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AppointmentAddLogic {
	return &AppointmentAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AppointmentAddLogic) AppointmentAdd(in *homerpc.AppointmentAddRequest) (*homerpc.AppointmentAddResponse, error) {
	// todo: add your logic here and delete this line

	parse, _ := time.Parse(time.DateTime, in.Time)

	var apponitment model.Appointment
	model.DB.Debug().Where("house_id", in.HouseId).Where("time=?", in.Time).Find(&apponitment)
	if apponitment.ID != 0 {

		return nil, fmt.Errorf("该时间已被预约")
	}
	add := parse.Add(-time.Hour * 8)
	apponitment = model.Appointment{
		Time:    add,
		UserId:  int(in.UserId),
		HouseId: int(in.HouseId),
		Name:    in.Name,
		Tel:     in.Tel,
	}
	model.DB.Debug().Create(&apponitment)
	return &homerpc.AppointmentAddResponse{
		AddId: int64(apponitment.ID),
	}, nil
}
